home *** CD-ROM | disk | FTP | other *** search
/ The Best of MacTutor - S…e Code for Volumes 1 to 5 / The Best of MacTutor - Source Code for Volume 1-5 (Wayzata Technology)(6031)(1990).bin / Source Code / #44 (May 89) / DA.sit / UselessDA.c < prev    next >
C/C++ Source or Header  |  1988-06-04  |  4KB  |  209 lines

  1. #include    <MacTypes.h>
  2. #include    <MemoryMgr.h>
  3. #include    <Quickdraw.h>
  4. #include    <WindowMgr.h>
  5. #include    <EventMgr.h>
  6. #include    <OSUtil.h>
  7. #include    <ResourceMgr.h>
  8. #include    <ToolboxUtil.h>
  9. #include    <DeviceMgr.h>
  10.  
  11. #define        NULL        (0L)
  12.  
  13. #define    drvrOpen    0
  14. #define    drvrPrime    1
  15. #define    drvrControl    2
  16. #define    drvrStatus    3
  17. #define    drvrClose    4
  18.  
  19. #define    subidProc    0
  20. enum {
  21.     irtnOpenWindow = 0,
  22.     irtnDrawWindow,
  23.     irtnKillWindow
  24. };
  25.  
  26. extern short        main(CntrlParam*,DCtlPtr,short);
  27. extern short        DAOpen(CntrlParam*,DCtlPtr);
  28. extern short        DAClose(CntrlParam*,DCtlPtr);
  29. extern WindowPtr    OpenWindow(char*,short,short,short,short);
  30. extern void            DrawWindow(WindowPtr,char*);
  31. extern void            KillWindow(WindowPtr);
  32.  
  33. extern Boolean        FLoadProc(short,Ptr*);
  34. extern void            UnloadProc(Ptr);
  35.  
  36. Ptr        pProc;
  37. short    rsidBase;
  38.  
  39. #define        JumpToProc(pPROC,irtn,cwArgs)                                    \
  40.     asm {                                                                    \
  41.     unlk        a6                    /* get rid of LSC's frame             */    \
  42.     movea.l        sp,a0                /* address of first argument        */    \
  43.     lea            -8(sp),sp            /* make room on the stack             */    \
  44.     movea.l        sp,a1                /* bottom of stack                    */    \
  45.     move.w        #(cwArgs)+1,d0        /* (# of words to copy)-1            */    \
  46.     /* copy rtn addr and arguments      */                                    \
  47. LCopyAnother:                                                                \
  48.     move.w        (a0)+,(a1)+            /* move a word down                    */    \
  49.     dbra        d0,@LCopyAnother    /* done when --d0 == -1                */    \
  50.     move.l        a4,(a1)+            /* copy our a4 into storage            */    \
  51.     move.l        (sp)+,(a1)            /* copy our rtn addr just behind it    */    \
  52.                                                                             \
  53.     pea            @LReturnToCaller    /* push return address for procedure*/    \
  54.     move.w        #irtn,-(sp)            /* push routine selector            */    \
  55.     move.l        pPROC,-(sp)            /* push address of PROC                */    \
  56.     beq.s        @LHandleError        /* jump if passed a NULL pPROC        */    \
  57. LJumpToProc:                                                                \
  58.     rts                                /* jump to proc    (sleazy trick)        */    \
  59.                                                                             \
  60. LHandleError:                                                                \
  61.     dc.w        0xA9FF                /* let Macsbug "handle" the error    */    \
  62.     lea            10(sp),sp            /* fix up the stack                    */    \
  63.                                                                             \
  64. LReturnToCaller:                                                            \
  65.     lea            (2*(cwArgs))(sp),a0    /* get address of our storage space    */    \
  66.     movea.l        (a0)+,a4            /* restore our a4                    */    \
  67.     movea.l        (a0),a0                /* get our return address            */    \
  68.     lea            8(sp),sp            /* fix up the stack                    */    \
  69.     jmp            (a0)                /* return to sender (address known)    */    \
  70.     }
  71.  
  72. short
  73. main(piopb, pdce, drvrRoutine)
  74. CntrlParam    *piopb;
  75. DCtlPtr        pdce;
  76. short        drvrRoutine;
  77. {
  78.     GrafPtr        pgpSav;
  79.     short        valRtn;
  80.     
  81.     if (pdce->dCtlStorage == NULL) {
  82.         if (drvrRoutine == 0) {    /*  open, but no data  */
  83.             SysBeep(3);
  84.             CloseDriver(pdce->dCtlRefNum);
  85.         }
  86.         return(0);
  87.     }
  88.     
  89.     GetPort ( &pgpSav );
  90.  
  91.     switch (drvrRoutine) {
  92.     case drvrOpen:
  93.         valRtn = DAOpen(piopb,pdce);
  94.         break;
  95.     case drvrControl:
  96.     case drvrPrime:
  97.     case drvrStatus:
  98.         valRtn = 0;
  99.         break;
  100.     case drvrClose:
  101.         valRtn = DAClose(piopb,pdce);
  102.         break;
  103.     }
  104.     
  105.     SetPort ( pgpSav );
  106.     
  107.     return    valRtn;
  108.     
  109. } /* main */
  110.  
  111. short
  112. DAOpen(piopb,pdce)
  113. CntrlParam    *piopb;    /* pointer to parameter block */
  114. DCtlPtr        pdce;    /* the device control entry */
  115. {
  116.     rsidBase = 0xC000 | (~pdce->dCtlRefNum<<5);
  117.     
  118.     if ( !FLoadProc ( 0, &pProc ) ) {
  119.         SysBeep(2);
  120.         CloseDriver(pdce->dCtlRefNum);
  121.         return 0;
  122.     }
  123.  
  124.     /* Using hard-wired window coordinates? For shame... */
  125.     pdce->dCtlWindow=OpenWindow("\POur Window",100,50,300,150);
  126.  
  127.     ((WindowPeek)pdce->dCtlWindow)->windowKind=pdce->dCtlRefNum;
  128.     
  129.     DrawWindow(pdce->dCtlWindow,"\PSomething funny.");
  130.     
  131.     return 0;
  132.  
  133. } /* DAOpen */
  134.  
  135. short
  136. DAClose(piopb,pdce)
  137. CntrlParam    *piopb;
  138. DCtlPtr        pdce;
  139. {
  140.     if ( pdce->dCtlWindow != NULL )
  141.         KillWindow ( pdce->dCtlWindow );
  142.  
  143.     if ( pProc != NULL )
  144.         UnloadProc ( pProc );
  145. }
  146.  
  147. Boolean
  148. FLoadProc ( subid, ppProc )
  149. short    subid;
  150. Ptr        *ppProc;
  151. {
  152.     Handle    hProc;
  153.  
  154.     hProc = GetResource ( 'PROC', rsidBase + subid );
  155.     if ( hProc == NULL ) {
  156.         *ppProc = NULL;
  157.         return FALSE;
  158.     }
  159.  
  160.     /* avoid fragmentation and other evils */
  161.     MoveHHi ( hProc );
  162.     HLock ( hProc );
  163.     HNoPurge ( hProc );
  164.     *ppProc = *hProc;
  165.     return TRUE;
  166. }
  167.  
  168. void
  169. UnloadProc ( pProc )
  170. Ptr        pProc;
  171. {
  172.     Handle    hProc;
  173.     
  174.     if ( pProc == NULL )
  175.         return;
  176.     
  177.     hProc = RecoverHandle ( pProc );
  178.     if ( hProc == NULL )
  179.         return;
  180.     HUnlock ( hProc );
  181.     HPurge ( hProc );
  182.     return;
  183. }
  184.  
  185. WindowPtr
  186. OpenWindow ( stTitle, xLeft, yTop, xRight, yBottom )
  187. char    stTitle[];
  188. short    xLeft, yTop, xRight, yBottom;
  189. {
  190.     JumpToProc(pProc,irtnOpenWindow,6);
  191. }
  192.  
  193. void
  194. DrawWindow(pwind,st)
  195. WindowPtr    pwind;
  196. char        st[];
  197. {
  198.     JumpToProc(pProc,irtnDrawWindow,4);
  199. }
  200.  
  201. void
  202. KillWindow(pwind)
  203. WindowPtr    pwind;
  204. {
  205.     JumpToProc(pProc,irtnKillWindow,2);
  206. }
  207.  
  208.  
  209.